package cn.gjculture.shopping.api.service.impl;

import cn.gjculture.shopping.api.dao.CashCouponDao;
import cn.gjculture.shopping.api.dao.CommonDeleteMapper;
import cn.gjculture.shopping.api.dao.CommonUpdateMapper;
import cn.gjculture.shopping.api.entity.CashCoupon;
import cn.gjculture.shopping.api.service.CashCouponService;
import cn.gjculture.shopping.api.util.DateUtil;
import cn.gjculture.shopping.api.util.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CashCouponServiceImpl implements CashCouponService {

    @Autowired
    CashCouponDao cashCouponDao;

    @Autowired
    CommonDeleteMapper commonDao;

    @Autowired
    CommonUpdateMapper commonUpdateDao;

    @Override
    public int insert(CashCoupon cashCoupon) {
        cashCoupon.setCashCouponId(UUIDUtil.getUUID());
        cashCoupon.setInsertTime(DateUtil.getTimestamp());
        cashCoupon.setUpdateTime(DateUtil.getTimestamp());
        return cashCouponDao.insert(cashCoupon);
    }

    @Override
    public List<CashCoupon> query() {
        List<CashCoupon> cashCoupons=cashCouponDao.query();
        return cashCoupons;
    }

    @Override
    public CashCoupon queryById(String cashCouponId) {
        CashCoupon cashCoupon=cashCouponDao.queryById(cashCouponId);
        return cashCoupon;
    }

    @Override
    public int deleteMore(List<String> cashCouponIds) {
        return commonDao.deleteCashCoupon(cashCouponIds);
    }

    @Override
    public int update(CashCoupon address) {
        address.setUpdateTime(DateUtil.getTimestamp());
        return commonUpdateDao.updateCashCoupon(address);
    }

    @Override
    public List<CashCoupon> queryByCustomerId(String customerId) {
        List<CashCoupon> cashCoupons= cashCouponDao.queryByCustomerId(customerId);
        for(CashCoupon cc:cashCoupons){
            cc.setStartValid(DateUtil.getDate(cc.getStartTime()));
            cc.setEndValid(DateUtil.getDate(cc.getEndTime()));
        }
        return cashCoupons;
    }
}
